package imageviewer.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import imageviewer.domain.PicData;
import imageviewer.beans.PicBean;

public class PictureServlet extends HttpServlet{

	public void doPost (HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		res.setContentType("image/jpeg");
		PicBean bean = new PicBean();
		Long id = null;
		try {
			id = Long.valueOf(req.getParameter("id")).longValue();
		} catch (NumberFormatException e) {
			return;
		}
		bean.setId(id);
		bean.fetchPic();
		PicData pic = bean.getPic();
		OutputStream out = res.getOutputStream();
		out.write(pic.getPic());
		out.flush();
		out.close();
	}

	public void doGet (HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {

		doPost(req,res);

	}
}
